package bb.lanxing.activity.routebook;

import com.github.mikephil.charting.utils.Utils;
import com.umeng.analytics.MobclickAgent;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import bb.lanxing.App;
import bb.lanxing.R;
import bb.lanxing.activity.base.BaseActivity;
import bb.lanxing.common.config.UmengEventConst;

public class RouteBookPositionSearchActivity extends BaseActivity {
    private static final int REQUESTCODE_ENDPOINT_CHOOSE = 2;
    private static final int REQUESTCODE_STARTPOINT_CHOOSE = 1;
    private RelativeLayout chooseEnd;
    private RelativeLayout chooseStart;
    private ImageView deleteEnd;
    private ImageView deleteStart;
    private double endLat;
    private double endLng;
    private RelativeLayout endPosition;
    private TextView endText;
    private Button searchBtn;
    private TextView starText;
    private double startLat;
    private double startLng;
    private RelativeLayout startPosition;

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_lushu_start_position_search);
        setupActionBar(true);
        setTitle(R.string.title_activity_lushu_search);
        bindView(getWindow().getDecorView());
        this.chooseStart.setOnClickListener(v -> {
            Intent intent2 = new Intent(this, RouteBookPositionChooseActivity.class);
            intent2.putExtra("style", 1);
            startActivityForResult(intent2, REQUESTCODE_STARTPOINT_CHOOSE);
        });
        this.chooseEnd.setOnClickListener(v -> {
            Intent intent = new Intent(this, RouteBookPositionChooseActivity.class);
            intent.putExtra("style", 2);
            startActivityForResult(intent, REQUESTCODE_ENDPOINT_CHOOSE);
        });
        this.deleteStart.setOnClickListener(v -> {
            this.starText.setText("");
            this.startPosition.setVisibility(View.GONE);
        });
        this.deleteEnd.setOnClickListener(v -> {
            this.endText.setText("");
            this.endPosition.setVisibility(View.GONE);
        });
        this.searchBtn.setOnClickListener(v -> {
            if (!this.startPosition.isShown() && !this.endPosition.isShown()) {
                App.getContext().showMessage(R.string.lushu_point_select_toast);
                return;
            }
            MobclickAgent.onEventValue(this, UmengEventConst.LUSHU_SEARCH_START_END, null, 1);
            Intent intent3 = new Intent(this, RouteBookSearchResultActivity.class);
            intent3.putExtra("style", 2);
            intent3.putExtra("startLat", this.startLat);
            intent3.putExtra("startLng", this.startLng);
            intent3.putExtra("endLat", this.endLat);
            intent3.putExtra("endLng", this.endLng);
            startActivity(intent3);

        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == REQUESTCODE_STARTPOINT_CHOOSE) {
                this.startLat = intent.getDoubleExtra("lat", Utils.DOUBLE_EPSILON);
                this.startLng = intent.getDoubleExtra("lng", Utils.DOUBLE_EPSILON);
                this.starText.setText(intent.getStringExtra("address"));
                this.startPosition.setVisibility(View.VISIBLE);
            } else if (requestCode == REQUESTCODE_ENDPOINT_CHOOSE) {
                this.endLat = intent.getDoubleExtra("lat", Utils.DOUBLE_EPSILON);
                this.endLng = intent.getDoubleExtra("lng", Utils.DOUBLE_EPSILON);
                this.endText.setText(intent.getStringExtra("address"));
                this.endPosition.setVisibility(View.VISIBLE);
            }
        }
    }

    private void bindView(View view) {
        this.chooseStart = view.findViewById(R.id.chooseStart);
        this.startPosition = view.findViewById(R.id.startPosition);
        this.chooseEnd = view.findViewById(R.id.chooseEnd);
        this.endPosition = view.findViewById(R.id.endPosition);
        this.searchBtn = view.findViewById(R.id.searchBtn);
        this.deleteEnd = view.findViewById(R.id.deleteEnd);
        this.deleteStart = view.findViewById(R.id.deleteStart);
        this.starText = view.findViewById(R.id.starText);
        this.endText = view.findViewById(R.id.endText);
    }
}
